package com.example.springboot08.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot08.entity.Clazz;
import com.example.springboot08.entity.Student;
import com.example.springboot08.service.ClazzService;
import com.example.springboot08.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/clazz")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private StudentService studentService;

    //查询所有班级信息
    @RequestMapping("/findAll")
    public List<Clazz> findAllClazz() {
        return clazzService.list(null);
    }

    //通过id查询班级
    @RequestMapping("/findById")
    public Clazz findClazzById(@RequestParam Long id) {
        return clazzService.getById(id);
    }

    //添加班级信息
    @RequestMapping("/save")
    public boolean saveClazz(@RequestBody Clazz clazz) {
        return clazzService.save(clazz);
    }

    //修改班级信息
    @RequestMapping("/update")
    public boolean updateClazzById(@RequestBody Clazz clazz) {
        return clazzService.updateById(clazz);
    }


    //根据id删除班级
    @RequestMapping("/removeById")
    public boolean removeClazz(@RequestParam Long id) {
        //返回Boolean类型
        return clazzService.removeById(id);
    }

    //批量删除班级
    @RequestMapping("/removeByIds")
    public boolean removeClazzByIds(@RequestBody List<Integer> ids) {
        return clazzService.removeByIds(ids);
    }

    //查询所有班级信息和对应的学生信息
    @RequestMapping("/findClazzAndStudent")
    public List<Clazz> findClazzAndStudent() {
        List<Clazz> clazzList = clazzService.list(null);
        for (Clazz clazz : clazzList) {
            QueryWrapper<Student> wrapper = new QueryWrapper<>();
            wrapper.eq("cid",clazz.getCid());
            List<Student> studentList = studentService.list(wrapper);
            clazz.setStudentList(studentList);
        }
       return clazzList;
    }
}

